import Cookies from 'js-cookie'

function isJSON_test(str) {
	if (typeof str == 'string') {
		try {
			var obj = JSON.parse(str);
			//console.log('转换成功：', obj);
			return obj;
		} catch (e) {
			//console.log('error：' + str + '!!!' + e);
			return str;
		}
	}
	//console.log('It is not a string!')
}
const TokenKey = 'vue_admin_template_tokens'

export function getToken(cookiesName = TokenKey) {
	return Cookies.get(cookiesName)
}


export function getLoc(locName = 'user') {
	//console.log(window.localStorage.getItem(locName))
	var loc = localStorage[locName]

	var obj =  isJSON_test(loc)
	return obj
}
export function setLoc(locName = 'user', locVal) {
	//console.log('sdasgg',locVal)
	locVal = window.localStorage.setItem(locName, typeof(locVal) === 'object' ? JSON.stringify(locVal) : locVal)
}
export function removeLoc(locName = 'user') {
	return window.localStorage.removeItem(locName)
}
export function setToken(token, cookiesName = TokenKey) {
	return Cookies.set(cookiesName, token)
}

export function removeToken(cookiesName = TokenKey) {
	return Cookies.remove(cookiesName)
}
